<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$THEME->addBreadcrumb($CONFIG->adminUrl.'/login', $this->lang('ap_title_login'));

$VIEW->template = 'admin-login.tpl';

if(get('force-login')==1){
    $VIEW->template = 'admin-thickbox.tpl';
}


if(Antz_IntelliForm::submitted('admin-login')){
    $error = false;
    $username = post('username');
    $password = post('password');
    $username = $DB->escape($username);

    $uData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}admin_users WHERE username = '{$username}'");
    if(!is_array($uData) || count($uData)==0){
        JFX::addError('Invalid username');
        $error = true;
        JFX::addFormError('username');
    }else{
        // found the user
        if($CRYPT->decrypt($uData['pass']) !== $password){
            $error = true;
            JFX::addError('Invalid password');
            JFX::addFormError('password');
        }
    }
    if(!$error){
        $USER->login($uData);
        $USER->setAdmin(true);
        if(get('force-login')!=1) JFX::redirect(THIS_PAGE_URL);
        else{
            $VIEW->addJs('parent.tb_remove()');
            JFX::addSuccess('Session restored, you may close the window now');
        }
    }
}else if(get('force-login')==1){
    JFX::addError($this->lang('session_expired_force_login'));
}

// assemble the form
$formFields = array(
    array(
        'name' => 'username',
        'value' => post('username'),
        'label' => 'Username'

    ),
    array(
        'name' => 'password',
        'value' => '',
        'label' => 'Password',
        'type' => 'password'
    ),
    array(
        'name' => 'submit',
        'value' => 'Login',
        'label' => '&nbsp;',
        'type' => 'submit'
    )
);

$form = JFX::makeRapidForm($formFields, 'Login to Control Panel', 'admin-login');
$form->setId('JFXAdminForceLogin');
$SMARTY->assign('loginForm', $form->render());
$VIEW->addTemplate('main', 'admin/login-form.tpl');